﻿using System;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Enyim.Caching;
using SilenthinkContract;
using SilenthinkContract.Domain;

namespace Web.Manage.Controllers
{
    public class ProductAttributeNameController : BaseController
    {
        public ProductAttributeNameController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient) : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(string productCategoryCode = "", string parentAttributeNameCode = "")
        {
            var model =StoreService.GetProductAttributeName(productCategoryCode, parentAttributeNameCode).OrderBy(o => o.AttributeNameCode).ToList();
            ViewBag.ProductCategoryCode = productCategoryCode;
            return View(model);
        }

        public ActionResult Create(string productCategoryCode = "", string parentAttributeNameCode = "")
        {
            ViewBag.ProductCategoryCode = productCategoryCode;
            ViewBag.ParentAttributeNameCode = parentAttributeNameCode;
            return View();
        }
        [HttpPost]
        public ActionResult Create(ProductAttributeName model)
        {
            if (string.IsNullOrEmpty(model.ParentAttributeNameCode))
            {
                model.ParentAttributeNameCode = string.Empty;
                model.ParentAttributeValueCode = string.Empty;
                model.ChildAttributeNameCode = string.Empty;
                model.AttributeNameLevel = 1;
                model.HasChild = false;
            }
            var date = DateTime.Now;
            model.CreatedBy = "admin";
            model.CreatedOn = date;
            var result = StoreService.CreateProductAttributeName(model);

            return Json(result ? HttpStatusCode.OK : HttpStatusCode.Ambiguous, result ? "添加成功" : "添加失败", "/ProductAttributeName/", "/ProductAttributeName/?productCategoryCode="+model.ProductCategoryCode);
        }
    }
}
